package com.awx.client.navigation;

import com.awx.client.login.AwxLoginWindow;
import com.awx.client.navigation.menu.AwxMenuConfiguration;

public abstract class AwxApplication {

	private AwxLoginWindow loginWindow;
	private AwxController controller;
	private AwxMenuConfiguration menuConfiguration;


	public void startUp() {
		this.configureLoginWindow();
		this.configureController();

		this.showLoginWindow();
	}

	private void configureController() {
		//get configured controller
		this.controller = this.getController();
		this.controller.setApplication( this );
	}

	private void configureLoginWindow() {
		//get configured login window
		this.loginWindow = this.getLoginWindow();
		if( this.loginWindow != null ) {
			this.loginWindow.setApplication( this );
		}
	}

	/**
	 * Show the login window if needed
	 *
	 */
	private void showLoginWindow() {

		//if any, go to that window
		if( this.loginWindow != null ) {
			this.loginWindow.setVisible( true );

		//if not, go directly to the main menu
		} else {
			this.goToMainMenu();
		}
	}

	/**
	 * Register the login window of the application. If it is not necesarry, return null
	 * @return Login window of the application or null
	 */
	protected abstract AwxLoginWindow getLoginWindow();


	/**
	 * Register the application controller
	 * @return Application controller
	 */
	protected abstract AwxController getController();


	/**
	 * Show the main menu of the application
	 * @param configuration
	 */
	public void goToMainMenu() {
		if( this.loginWindow != null ) {
			this.loginWindow.setVisible( false );
			this.loginWindow.dispose();
		}
		this.controller.startUp();
	}

	/**
	 * Sets the configuration of the main menu
	 * @param menuConfiguration
	 */
	public void setMenuConfiguration(AwxMenuConfiguration menuConfiguration) {
		this.menuConfiguration = menuConfiguration;
	}


	/**
	 * @return the menu configuration
	 */
	public AwxMenuConfiguration getMenuConfiguration() {
		return menuConfiguration;
	}


}





